question =null;
zm.ready(function() {

    isshow = false;

    zm(".add-item").click(function() {

        if (isshow === false) {
            zm(".question").find("input").each(function() {
                zm(this).val("");
            });
            parent = zm('.form-question');
            parent.after(getQuestionForm());
            zm('.question').addClass('boder-style');
            AddQuestionEvent();
            isshow = true;
        }
        // zm(".question").show();
    });
    zm("#save").click(function() {
        // find title and decription form

        var title = zm('.header-form input').val();
        var descripton = zm('.header-form textarea').val();
        var share = zm(".share").attr("checked");
        var start = zm('#date-start').val().split("/");
        var end = zm('#date-end').val().split("/");
        var sday = start[0];
        var smonth = start[1];
        var syear = start[2];
        var eday = end[0];
        var emonth = end[1];
        var eyear = end[2];
        var point = zm('#point').val();
        var beginDate = new Date();
        beginDate.setDate(sday);
        beginDate.setMonth(smonth - 1);
        beginDate.setFullYear(syear);
        var endDate = new Date();
        endDate.setDate(eday);
        endDate.setMonth(emonth - 1);
        endDate.setFullYear(eyear);
        var benginDay = parseInt(beginDate.getTime() / 1000);
        var endDay = parseInt(endDate.getTime() / 1000);
        var survey = new Survey(title, descripton, benginDay, endDay, point, share);
        // find question form
        var index = 1;
        zm('.question-answer').each(function() {

            var title = zm(this).find('.question-title').text();
            var helpText = zm(this).find('.question-helptext').text();
            var type = zm(this).find('.answer').attr("typeanswer");
            var require = zm(this).find('.require').attr("requirement");

            var question = new Question(index, title, helpText, type, require);
            if (type === singleselection || type === multichoice) {

                zm(this).find('.answer .answer-option').each(function() {
                    var opption = zm(this).find('span').text();
                    console.log(opption);
                    question.addChoices(opption);
                });
            }
            survey.addToQuestions(question);
            index++;
        });
        var error = checkSurvey(survey);
        if (error !== "") {
            zm.Boxy.alert(error, "Lỗi nhập liệu");
            return;
        }
        var JSONSurvey = zm.serialize(survey);
        ajaxAddSurvey(JSONSurvey);

    });
});

function AddQuestionEvent() {
    // select combobox event
    zm("#choice-answer").change(function() {
        var type = zm(this).val();
        if (type === singleline) {
            zm(".content-answer").html(getText());
        }
        if (type === multiline) {
            zm(".content-answer").html(getParagraph());
        }
        if (type === singleselection || type === multichoice) {

            zm(".content-answer").html(getMultiChoice());
            // and event for button add
            zm(".add-option").bind("click", function() {
                zm(".add-option").before(getChoice());
                //removeButton();
                zm(".remove").unbindAll("click");
                zm(".remove").bind("click", function() {

                    zm(this).parent().remove();
                });
            });
        }
    });

    //cancel button

    zm(".cancel-question").click(function() {
        parent.show();
        zm(".question").remove();
        isshow = false;
    });

    // done question
    zm(".done-question").bind("click", function() {
        var html = "";
        if (!isEdit) {
            html = addquestion();
            if (html !== "")
                zm(".form-question").append(html);
            else
                return;
            isEdit = false;
        }
        else {
            html = addeditedquestion();
            if (html !== ""){
                zm('.question').after(html);
                parent.remove();
            }
                
            else
                return;
            isEdit = false;

        }
        isshow = false;

        // raise delete question event
        zm('.remove-answer').unbindAll("click");
        zm('.remove-answer').bind("click", function() {
            question = zm(this).parent().parent();
            new zm.Boxy({
                title: 'Nhắc nhở',
                content: 'Bạn có chắc xóa câu hỏi đã chọn?',
                modal: false,
                okButton: 'Đồng ý',
                cancelButton: 'Hủy bỏ',
                //s  onCancel: close,
                onOk: removeQuestion
            }).show();
//            console.log("xoas");
//            zm(this).parent().parent().remove();
//            var i = 1;
//            zm(".form-question").find(".question-answer").each(function() {
//                zm(this).find(".question-span").text("Câu " + i + ": ");
//                i++;
//            });

        });

        // raise edit question event
        zm('.edit-answer').unbindAll("click");
        zm('.edit-answer').bind("click", function() {
            if (isshow === false) {
                isEdit = true;
                parent = zm(this).parent().parent();
                editquestion();
                zm('.question').addClass('isedit');
                isshow = true;
            }
            // zm(this).parent().parent().;
        });
        zm(".question").remove();

    });

}
function removeQuestion() {
    question.remove();
    var i = 1;
    zm(".form-question").find(".question-answer").each(function() {
        zm(this).find(".question-span").text("Câu " + i + ": ");
        i++;
    });


}


function addquestion() {
    zm(".form-question").addClass("boder-style");
    var require = zm('input[name="require"]').attr("checked");
    var title = zm('input[name="title"]').attr("value");
    if (title === null || title === "") {
        zm.Boxy.alert("Hãy nhập nội dung câu hỏi!", "Lỗi nhập liệu");
        return "";
    }
    var help = zm('input[name="help-text"]').attr("value");
    var type = zm('#choice-answer').val();
    var num = zm(".form-question").find(".question-answer").length + 1;
    var question = '<div class="question-answer" >';
    var temp = true;
    question += '<div class="edit non-display"><button class="remove-answer" >Xóa</button>';

    question += '<button class="edit-answer " >Sửa</button></div><span class="question-span">Câu ' + num + ':</span><span class="question-title">' + title + "</span>";
    if (require === true) {
        question += '<span  class="require" requirement=true>(*)</span>';
    }
    else {
        question += '<span class="require" requirement=false></span>';
    }

    question += '<p class="question-helptext">' + help + '</p>';
    if (type === singleline) {
        question += '<div class="answer" typeanswer="' + singleline + '">';

        question += '<input type="text" class="in-tex-ans" size="50"/>';
    }
    if (type === multiline) {
        question += '<div class="answer" typeanswer="' + multiline + '">';
        question += '<textarea cols="50" class="in-are-ans"  ></textarea>';

    }
    if (type === multichoice) {
        question += '<div class="answer" typeanswer="' + multichoice + '">';
        zm(".option").each(function() {
            var txt = zm(this).find('input[type="text"]').val();
            if (txt === null || txt === "") {
                temp = false;
                return;
            }
            question += '  <div class="answer-option">'
                    + '<input type="checkbox" ><span>' + txt + '</span>'
                    + '</div>';
        });
    }
    if (type === singleselection) {
        question += '<div class="answer" typeanswer="' + singleselection + '">';
        var name = radioName++;
        zm(".option").each(function() {
            var txt = zm(this).find('input[type="text"]').val();
            if (txt === null || txt === "") {
                temp = false;
                return;
            }
            question += '<div class="answer-option" >'
                    + '<input type="radio" name="' + name.toString() + '"><span>' + txt + '</span>'
                    + '</div>';
        });

    }
    if (temp === false) {
        zm.Boxy.alert("Hãy nhập nội dung lựa chọn!", "Lỗi nhập liệu");
        return "";
    }
    question += '</div>';
    question += '</div>';
    return question;

}

function addeditedquestion() {
    zm(".form-question").addClass("boder-style");
    var require = zm('input[name="require"]').attr("checked");
    var title = zm('input[name="title"]').attr("value");
    var help = zm('input[name="help-text"]').attr("value");
    var type = zm('#choice-answer').val();
    var num = parent.find(".question-span").text();
    var question = '<div class="question-answer">';
    if (title === null || title === "") {
        zm.Boxy.alert("Hãy nhập nội dung câu hỏi!", "Lỗi nhập liệu");
        return "";
    }
    var temp = true;
    question += '<div class="edit"><button class="remove-answer" >Xóa</button>';

    question += '<button class="edit-answer " >Sửa</button></div><span class="question-span">' + num + '</span><span class="question-title">' + title + "</span>";
    if (require === true) {
        question += '<span  class="require" requirement=true>(*)</span>';
    }
    else {
        question += '<span class="require" requirement=false></span>';
    }

    question += '<p class="question-helptext">' + help + '</p>';
    if (type === singleline) {
        question += '<div class="answer" typeanswer="' + singleline + '">';

        question += '<input type="text" class="in-tex-ans" size="50"/>';
    }
    if (type === multiline) {
        question += '<div class="answer" typeanswer="' + multiline + '">';
        question += '<textarea cols="50" class="in-are-ans" ></textarea>';

    }
    if (type === multichoice) {
        question += '<div class="answer" typeanswer="' + multichoice + '">';
        zm(".option").each(function() {
            var txt = zm(this).find('input[type="text"]').val();
            if (txt === null || txt === "") {
                temp = false;
                return;
            }
            question += '  <div class="answer-option">'
                    + '<input type="checkbox" ><span>' + txt + '</span>'
                    + '</div>';
        });
    }
    if (type === singleselection) {
        question += '<div class="answer" typeanswer="' + singleselection + '">';
        var name = radioName++;
        zm(".option").each(function() {
            var txt = zm(this).find('input[type="text"]').val();
            if (txt === null || txt === "") {
                temp = false;
                return;
            }
            question += '<div class="answer-option" >'
                    + '<input type="radio" name="' + name.toString() + '"><span>' + txt + '</span>'
                    + '</div>';
        });


    }
    if (temp === false) {
        zm.Boxy.alert("Hãy nhập nội dung lựa chọn!", "Lỗi nhập liệu");
        return "";
    }
    question += '</div></div>';
    return question;

}

function showeditquestion(title, help, type, require, value) {
    parent.after(getQuestionForm());
    AddQuestionEvent();
    var temp = false;
    if (require === "true")
        temp = true;
    zm('input[name="require"]').attr("checked", temp);
    zm('input[name="title"]').attr("value", title);
    zm('input[name="help-text"]').attr("value", help);
    var temp = "";
    zm('#choice-answer').val(type);
    temp += '<span class="bcl1">&nbsp;</span>';
    temp += '<span class="bcr1">';
    for (var i = 0; i < value.length; i++) {
        temp += '<div class="option"><input type="radio" name="0" disabled=""><input type="text" size="50" value="' + value[i] + '"/><button class="remove">Xóa</button><br></div>';
    }
    if (type === singleselection || type === multichoice) {
        temp += '<button class="add-option button-zm1">Thêm</button>';
    }
    temp += '</span><div class="clr"><div>';

    zm(".content-answer").html(temp);
    // and event for button add
    zm(".add-option").bind("click", function() {
        zm(".add-option").before(getChoice());
        //removeButton();
        zm(".remove").unbindAll();
        zm(".remove").bind("click", function() {
            zm(this).parent().remove();
            //removeButton();
        });
        //   parent.remove();
        parent.hide();
        // zm(".question").show();
    });

    zm(".remove").unbindAll();
    zm(".remove").bind("click", function() {
        zm(this).parent().remove();
        //removeButton();
    });
    zm('.question').addClass('isedit');
    parent.hide();
   // parent.remove();
    // zm(".question").show();
}

function editquestion() {

    var title = parent.find(".question-title").text();
    var help = parent.find(".question-helptext").text();
    var type = parent.find(".answer").attr("typeanswer");
    var require = parent.find(".require").attr("requirement");
    var value = new Array();
    var i = 0;
    parent.find(".answer-option").each(function() {
        var temp = zm(this).find("span").text();
        value[i++] = temp;
    });
    showeditquestion(title, help, type, require, value);
}

function removeButton() {
    var size = zm(".content-answer").children().size();
    if (size === 2) {
        zm(".remove").addClass("hide");
    }
    else {
        zm(".remove").removeClass("hide");
    }
}


